//
//  TicketViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/17.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "TicketViewController.h"
#import "UIButton+Dzm.h"
#import "TicketCell.h"
#import "BottomView.h"
#import "SaleTicketViewModel.h"

@interface TicketViewController ()
@property (strong, nonatomic) SaleTicketViewModel *saleTicketViewModel;


@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;

@property (weak, nonatomic) IBOutlet UIButton *addTicketBtn;

- (IBAction)addTicketAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITableView *mainTable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainTable_bottom;

@end

@implementation TicketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.saleTicketViewModel = [[SaleTicketViewModel alloc ] init];
    
    [self setLeftBtnItem];
    
    // 如果可以选择
    if (self.isSelect) {
        [CommoneTools changeConstraintView:self.topView constraintHeight:0.1];
        [self.topView setHidden:YES];
        [self createBottomBtn];
        self.mainTable_bottom.constant = 60;
        
        [self loadSelectedData];
        
        
    }else{
        self.inputTextField.layer.cornerRadius = 3;
        self.inputTextField.layer.masksToBounds=YES;
        self.inputTextField.leftView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 10, 10)];
        self.inputTextField.leftViewMode = UITextFieldViewModeAlways;
        [self.inputTextField setValue:GLOBLE_GRAY_COLOR_3 forKeyPath:@"_placeholderLabel.textColor"];
        [self.addTicketBtn styleButton];
        self.mainTable.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        [self loadNewData];
        
    }
  
  
    
    
}


-(void)loadSelectedData{
    
    self.saleTicketViewModel.LineType = self.LineType;
    self.saleTicketViewModel.SendTime = self.SendTime;
    self.saleTicketViewModel.OrderAmount = self.OrderAmount;
    
    WEAKSELF
    [self.saleTicketViewModel getMemberCouponsForOrder:^(NSInteger resultType, id data, NSString *errorMsg) {
        
        if (resultType == 1) {
            [weakSelf.mainTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg duration:1];
        }
    }];
}

-(void)loadData{
    
    WEAKSELF
    [self.saleTicketViewModel getMemberCouponsAll:^(NSInteger resultType, id data, NSString *errorMsg) {
       
        if (resultType == 1) {
            [weakSelf.mainTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg duration:1];
        }
    }];
    
}


-(void)loadNewData{
    self.saleTicketViewModel.pageIndex =1;
    [self loadData];

}

-(void)loadMoreData{
    self.saleTicketViewModel.pageIndex ++;
    [self loadData];
}

#pragma mark ---------------下方按钮------------------
-(void)createBottomBtn{
    NSMutableArray *bottomList = [[NSMutableArray alloc ] init];
     [bottomList addObject:@"不使用优惠券"];
    WEAKSELF
    BottomView *bottomView = [[BottomView alloc ] initWithStatusArray:bottomList callBlcok:^(NSInteger index) {
        
        if ([weakSelf.delegate respondsToSelector:@selector(selectedTicket:)]) {
            [weakSelf.delegate selectedTicket:nil];
        }
        [SVProgressHUD dismiss];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    UIView *view =  [[UIView alloc ] initWithFrame:CGRectMake(0, [UIScreen height] - 124, [UIScreen width], 60)];
    [view addSubview:bottomView];
    [self.view addSubview:view];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSelect) {
        
        if ([self.delegate respondsToSelector:@selector(selectedTicket:)]) {
            [self.delegate selectedTicket:[self.saleTicketViewModel.resultList objectAtIndex:indexPath.row]];
        }
        
        [SVProgressHUD dismiss];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.saleTicketViewModel.resultList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TicketCell *ticketCell = [tableView dequeueReusableCellWithIdentifier:@"TicketCell"];
    [ticketCell show:[self.saleTicketViewModel.resultList objectAtIndex:indexPath.row]];
    return ticketCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)addTicketAction:(UIButton *)sender {
    
    
}
@end
